package xyz.xxdl.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import xyz.xxdl.Service.AsdxService;
import xyz.xxdl.app.App;
import xyz.xxdl.pojo.Asdx;
import xyz.xxdl.pojo.Result;
import xyz.xxdl.pojo.Trade;
import xyz.xxdl.pojo.TradeGrids;

import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
public class AsdxController {
    @Autowired
    private AsdxService asdxService;
    @PostMapping("/submit")
    public Result submit(@RequestBody Asdx asdx){
        System.out.println("submit执行");
        App.asdx=asdx;
        // 创建线程并开启
        App app = new App();
        Thread t = new Thread(app,"刷量");
        t.start();

        return new Result(true,"开启成功");
    }
    @GetMapping("/status")
    public Result status(){
        List<Trade> list = App.list;
        if (list==null||list.size()==0){
            return new Result(false,"获取失败");
        }else {
            return new Result(true,"获取成功",list);
        }

    }
    @PutMapping("/info")
    public Result info(@RequestBody TradeGrids tradeGrids){
        Map<Thread, StackTraceElement[]> allStackTraces = Thread.getAllStackTraces();
        Set<Thread> threads = allStackTraces.keySet();
        for (Thread thread : threads) {
            String name = thread.getName();
            if ("刷量".equals(name)){
                return new Result(false,"刷量已启动,不能更改信息!");
            }
        }
        App.tradeGrids = tradeGrids;
        return new Result(true,"更新信息成功");
    }

    @GetMapping("/getInfo")
    public Result getInfo(){

        return new Result(true,"更新信息成功",new TradeGrids());
    }
}
